package com.generator.purchaseManage.productManage.service.impl;

import com.generator.purchaseManage.productManage.dao.IProductMapper;
import com.generator.purchaseManage.productManage.pojo.CProduct;
import com.generator.purchaseManage.productManage.service.IProductService;
import com.generator.purchaseManage.productTypeManage.pojo.CType;
import com.generator.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @auther wumei
 * @date 2022/10/06
 * 星期六 17:52
 */
@Service
public class ProductService implements IProductService {

    @Autowired
    private IProductMapper productMapper;

    public IProductMapper getProductMapper() {
        return productMapper;
    }
    public void setProductMapper(IProductMapper productMapper) {
        this.productMapper = productMapper;
    }

    /**
     * 根据查询条件查询商品信息
     * @param product
     * @return
     */
    @Override
    public PageModel<CProduct> queryProductByCondition(CProduct product, PageModel<CProduct> pageModel) {
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());
        List<CProduct> productList = productMapper.queryProductByCondition(product);


        PageInfo<CProduct> pageInfo = new PageInfo<CProduct>(productList);

        pageModel.setData(new ArrayList<CProduct>(productList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());

        return pageModel;
    }

    /**
     * 根据查询条件查询商品信息
     * @param product
     * @return
     */
    public CProduct queryProductByName(CProduct product){
        return productMapper.queryProductByName(product);
    }

    /**
     * 添加商品
     * @param product
     * @return
     */
    @Override
    public boolean addProduct(CProduct product) {
        // 判断当前新增的商品类型是否存在
        CProduct product1 = new CProduct();
        product1.setTypeId(product.getTypeId());
        product1.setProductModel(product.getProductModel());
        List<CProduct> cProducts = productMapper.queryProductByCondition(product1);
        if (cProducts.size() != 0){
            return false;
        }
        return productMapper.addProduct(product);
    }

    /**
     * 修改商品信息
     * @param product
     * @return
     */
    @Override
    public boolean updateProduct(CProduct product) {
        return productMapper.updateProduct(product);
    }

    /**
     * 删除商品
     * @param product
     * @return
     */
    @Override
    public boolean deleteProduct(CProduct product) {
        return productMapper.deleteProduct(product);
    }
}
